//
//  CollectMusicViewController.m
//  A_program
//
//  Created by 杨利佳 on 15/5/28.
//  Copyright (c) 2015年 BayMax. All rights reserved.
//

#import "CollectListViewController.h"
#import "NovelDatabase.h"
#import "CollectTableViewCell.h"
#import "NovelModel.h"
#import "NovelDatabase.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "NovelDetailsViewController.h"
#import "MusicThiViewController.h"
#import "NovelAlbumViewController.h"
#import "RadioThirdViewController.h"
#import "SecondListModel.h"

@interface CollectListViewController ()<UITableViewDataSource,UITableViewDelegate,CancleTiTleButtonDelegate>
@property (nonatomic, retain) UIButton *button;

@end

@implementation CollectListViewController
- (void)dealloc
{
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
- (void)loadView{
    [super loadView];
    [self createTableView];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
    [_button setTitle:@"清空收藏" forState:UIControlStateNormal];
    [_button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_button];

}

- (void)createTableView{
    self.collectTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    self.collectTableView.delegate = self;
    self.collectTableView.dataSource = self;
    [self.view addSubview:self.collectTableView];
    [self.collectTableView release];
}
//tableView协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.collectArray count];
}
    
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cella = @"cell";
    CollectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cella];
    if (!cell) {
        cell = [[CollectTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cella];
    }
    NovelModel *model = [self.collectArray objectAtIndex:indexPath.row];
    [cell.collectImageView sd_setImageWithURL:[NSURL URLWithString:model.coverLarge] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
    [cell createCellData:model];
    cell.cancelDelegate = self;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.cancelData isEqualToString:@"music"]) {
        MusicThiViewController *music = [[MusicThiViewController alloc]init];
        NovelModel *model = [self.collectArray objectAtIndex:indexPath.row];
        music.ID = [NSString stringWithFormat:@"%ld",model.albumIdMin];
        [self.navigationController pushViewController:music animated:YES];
    }else if ([self.cancelData isEqualToString:@"radio"]){
        RadioThirdViewController *radio = [[RadioThirdViewController alloc]init];
        NovelModel *model = [self.collectArray objectAtIndex:indexPath.row];
        radio.myId = [NSNumber numberWithInteger:model.albumIdMin];
        [self.navigationController pushViewController:radio animated:YES];   
        
    }else if ([self.cancelData isEqualToString:@"novel"]){
        NovelAlbumViewController *novel = [[NovelAlbumViewController alloc]init];
        NovelModel *model = [self.collectArray objectAtIndex:indexPath.row];
         novel.publicPath = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/%ld/true/1/15",model.albumIdMin];
        [self.navigationController pushViewController:novel animated:YES];
        
    }


}

//删除按钮的实现
- (void)createCancleTitleButton:(NSInteger)cancle
{
    NSUserDefaults *cancelStr = [NSUserDefaults standardUserDefaults];
    NovelModel *model = [[NovelModel alloc]init];
    model.albumId = cancle;
    NSString *loginName = [cancelStr objectForKey:@"loginName"];
    model.loginName = loginName;
    [NovelDatabase deleteWithModel:model];
    //刷新
    if ([self.cancelData isEqualToString:@"music"]) {
        self.collectArray = (NSMutableArray *)[NovelDatabase selectNamePlayKind:@"music" loginName:[cancelStr objectForKey:@"loginName"]];
    }else if ([self.cancelData isEqualToString:@"radio"]){
        self.collectArray = (NSMutableArray *)[NovelDatabase selectNamePlayKind:@"radio" loginName:[cancelStr objectForKey:@"loginName"]];
    }else if ([self.cancelData isEqualToString:@"novel"]){
        self.collectArray = (NSMutableArray *)[NovelDatabase selectNamePlayKind:@"novel" loginName:[cancelStr objectForKey:@"loginName"]];
        NovelDetailsViewController *novelDetails = [[NovelDetailsViewController alloc]init];
        [novelDetails.detailesFristTableView reloadData];
     
    }

    [self.collectTableView reloadData];
    
}

- (void)buttonAction:(id)sender
{
    if (_collectArray.count) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"是否确定清空收藏" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
        [alert release];
    } else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏夹中还没有东西哦!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        if (self.type == TableViewIsEditing) {
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
            NSString *loginName = [user objectForKey:@"loginName"];
            NovelModel *model = [[NovelModel alloc]init];
            model.loginName = loginName;
            if ([self.cancelData isEqualToString:@"music"]) {
                model.playKind = self.cancelData;
                [NovelDatabase deleteWithModelAllLogin:model];
            }else if ([self.cancelData isEqualToString:@"radio"]){
                model.playKind = self.cancelData;
                [NovelDatabase deleteWithModelAllLogin:model];
            }else if ([self.cancelData isEqualToString:@"novel"]){
                model.playKind = self.cancelData;
                [NovelDatabase deleteWithModelAllLogin:model];
            }
            
            //刷新
            NSUserDefaults *cancelAllStr = [NSUserDefaults standardUserDefaults];
            if ([self.cancelData isEqualToString:@"music"]) {
                
                
                self.collectArray = (NSMutableArray *)[NovelDatabase selectNamePlayKind:@"music" loginName:[cancelAllStr objectForKey:@"loginName"]];
            }else if ([self.cancelData isEqualToString:@"radio"]){
                self.collectArray = (NSMutableArray *)[NovelDatabase selectNamePlayKind:@"radio" loginName:[cancelAllStr objectForKey:@"loginName"]];
            }else if ([self.cancelData isEqualToString:@"novel"]){
                self.collectArray = (NSMutableArray *)[NovelDatabase selectNamePlayKind:@"novel" loginName:[cancelAllStr objectForKey:@"loginName"]];
                NovelDetailsViewController *novelDetails = [[NovelDetailsViewController alloc]init];
                [novelDetails.detailesFristTableView reloadData];
                
            }
            
            [self.collectTableView reloadData];
            
        }
        
    }
}



- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return YES;
    
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
